﻿using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 游戏创建 Componet 默认设置
/// </summary>
public static class GameCreateComponentSetting
{

    [MenuItem("GameObject/创建默认 UI/Image", priority = -201)]
    static void CreateImage()
    {
        if (Selection.activeTransform)
        {
            GameObject go = new GameObject("Img_", typeof(Image));
            Image image = go.GetComponent<Image>();
            image.raycastTarget = false;
            go.transform.SetParent(Selection.activeTransform, false);

            Selection.activeTransform = go.transform;
        }
    }

    [MenuItem("GameObject/创建默认 UI/Image", true)]
    static bool CreateImageValidate()
    {
        return Selection.activeTransform != null;
    }

    [MenuItem("GameObject/创建默认 UI/SuperImage", priority = -200)]
    static void CreateSuperImage()
    {
        if (Selection.activeTransform)
        {
            GameObject go = new GameObject("Img_", typeof(SuperImage));
            SuperImage image = go.GetComponent<SuperImage>();
            image.raycastTarget = false;
            go.transform.SetParent(Selection.activeTransform, false);

            Selection.activeTransform = go.transform;
        }
    }

    [MenuItem("GameObject/创建默认 UI/SuperImage", true)]
    static bool CreateSuperImageValidate()
    {
        return Selection.activeTransform != null;
    }

    [MenuItem("GameObject/创建默认 UI/Text", priority = -199)]
    static void CreateText()
    {
        if (Selection.activeTransform)
        {
            GameObject go = new GameObject("Txt_", typeof(Text));
            Text text = go.GetComponent<Text>();
            go.transform.localScale = Vector3.one * 0.5f;
            text.raycastTarget = false;
            text.supportRichText = false;
            text.font = Config.Default.Font;
            text.rectTransform.sizeDelta = Vector2.zero;
            text.alignment = TextAnchor.MiddleCenter;
            text.horizontalOverflow = HorizontalWrapMode.Overflow;
            text.verticalOverflow = VerticalWrapMode.Overflow;

            go.transform.SetParent(Selection.activeTransform, false);

            Selection.activeTransform = go.transform;
        }
    }

    [MenuItem("GameObject/创建默认 UI/Text", true)]
    static bool CreateTextValidate()
    {
        return Selection.activeTransform != null;
    }

    [MenuItem("GameObject/创建默认 UI/OutlineText", priority = -199)]
    static void CreateOutlineText()
    {
        if (Selection.activeTransform)
        {
            GameObject go = new GameObject("Txt_", typeof(Text));
            Text text = go.GetComponent<Text>();
            go.transform.localScale = Vector3.one * 0.5f;
            text.raycastTarget = false;
            text.supportRichText = false;
            text.font = Config.Default.Font;
            text.rectTransform.sizeDelta = Vector2.zero;
            text.alignment = TextAnchor.MiddleCenter;
            text.horizontalOverflow = HorizontalWrapMode.Overflow;
            text.verticalOverflow = VerticalWrapMode.Overflow;
            UGUIOutline outline = text.GetOrAddComponent<UGUIOutline>();
            outline.EffectDistance = outline.EffectDistance1 = new Vector2(-2.8f, 2.8f);
            go.transform.SetParent(Selection.activeTransform, false);

            Selection.activeTransform = go.transform;
        }
    }

    [MenuItem("GameObject/创建默认 UI/OutlineText", true)]
    static bool CreateOutlineTextValidate()
    {
        return Selection.activeTransform != null;
    }

    [MenuItem("GameObject/创建默认 UI/Button", priority = -198)]
    static void CreateButton()
    {
        if (Selection.activeTransform)
        {
            GameObject go = new GameObject("Btn_", typeof(Button));
            Button button = go.GetComponent<Button>();
            button.transition = Selectable.Transition.None;
            button.GetOrAddComponent<Image>();
            var effect = button.GetOrAddComponent<UIClickScaleEffect>();
            effect.TargetGo = button.gameObject;
            button.GetOrAddComponent<ButtonSound>();
            go.transform.SetParent(Selection.activeTransform, false);

            Selection.activeTransform = go.transform;

            CreateText();

            Selection.activeTransform = go.transform;
        }
    }

    [MenuItem("GameObject/创建默认 UI/Button", true)]
    static bool CreateButtonValidate()
    {
        return Selection.activeTransform != null;
    }

    [MenuItem("GameObject/创建默认 UI/NoTextButton", priority = -197)]
    static void CreateNoTextButton()
    {
        if (Selection.activeTransform)
        {
            GameObject go = new GameObject("Btn_", typeof(Button));
            Button button = go.GetComponent<Button>();
            button.transition = Selectable.Transition.None;
            button.GetOrAddComponent<Image>();
            var effect = button.GetOrAddComponent<UIClickScaleEffect>();
            effect.TargetGo = button.gameObject;

            go.transform.SetParent(Selection.activeTransform, false);

            button.GetOrAddComponent<ButtonSound>();

            Selection.activeTransform = go.transform;
        }
    }

    [MenuItem("GameObject/创建默认 UI/NoTextButton", true)]
    static bool CreateNoTextButtonValidate()
    {
        return Selection.activeTransform != null;
    }

    [MenuItem("GameObject/创建默认 UI/NonRectangularButtonImage", priority = -197)]
    public static void CreateNonRectAngularButtonImage()
    {
        var goRoot = Selection.activeGameObject;
        if (goRoot == null)
            return;

        var button = goRoot.GetComponent<Button>();

        if (button == null)
        {
            Logger.Log("Selecting Object is not a button!");
            return;
        }

        // 关闭原来button的射线检测
        var graphics = goRoot.GetComponentsInChildren<Graphic>();
        foreach (var graphic in graphics)
        {
            graphic.raycastTarget = false;
        }

        var polygon = new GameObject("NonRectangularButtonImage");
        polygon.AddComponent<PolygonCollider2D>();
        polygon.AddComponent<NonRectangularButtonImage>();
        polygon.transform.SetParent(goRoot.transform, false);
        polygon.transform.SetAsLastSibling();
    }

    [MenuItem("GameObject/创建默认 UI/NonRectangularButtonImage", true)]
    static bool CreateNonRectAngularButtonImageValidate()
    {
        return Selection.activeTransform != null;
    }


    [MenuItem("GameObject/创建默认 UI/Progress", priority = -195)]
    static void CreateProgress()
    {
        if (Selection.activeTransform)
        {
            var go = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Game/Repository/Prefabs/UIForms/_Common/UGUIProgress.prefab");
            go = Object.Instantiate(go);
            go.name = "Pro_";

            go.transform.SetParent(Selection.activeTransform, false);

            Selection.activeTransform = go.transform;
        }
    }

    [MenuItem("GameObject/创建默认 UI/Progress", true)]
    static bool CreateProgressValidate()
    {
        return Selection.activeTransform != null;
    }

    [MenuItem("GameObject/创建默认 UI/UGUIForm/UGUIForm", priority = -100)]
    static void CreateUGUIForm()
    {
        if (Selection.activeTransform)
        {
            GameObject go = new GameObject("Form", typeof(RectTransform));
            go.GetOrAddComponent<ScriptHelper>();
            go.tag = Constant.Tag.UIForm;

            go.transform.SetParent(Selection.activeTransform, false);

            Selection.activeTransform = go.transform;
        }
    }

    [MenuItem("GameObject/创建默认 UI/UGUIForm/UGUIForm", true)]
    static bool CreateUGUIFormValidate()
    {
        return Selection.activeTransform != null;
    }

    [MenuItem("GameObject/创建默认 UI/UGUIItem", priority = -99)]
    static void CreateUGUIItem()
    {
        if (Selection.activeTransform)
        {
            GameObject go = new GameObject("Item", typeof(RectTransform));
            go.GetOrAddComponent<ScriptHelper>();
            go.tag = Constant.Tag.UI;

            go.transform.SetParent(Selection.activeTransform, false);

            Selection.activeTransform = go.transform;
        }
    }

    [MenuItem("GameObject/创建默认 UI/UGUIItem", true)]
    static bool CreateUGUIItemValidate()
    {
        return Selection.activeTransform != null;
    }

    [MenuItem("GameObject/创建默认 UI/UGUIForm/TmpPageModel1", priority = -98)]
    static void CreateTmpPageModel1()
    {
        if (Selection.activeTransform)
        {
            var go = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Game/Repository/Prefabs/UIForms/_Common/TmpPageModel1.prefab");
            go = (GameObject)PrefabUtility.InstantiatePrefab(go);
            go.name = "PageContainer";

            go.transform.SetParent(Selection.activeTransform, false);

            Selection.activeTransform = go.transform;
        }
    }

    [MenuItem("GameObject/创建默认 UI/UGUIForm/TmpPageModel1", true)]
    static bool CreateTmpPageModel1Validate()
    {
        return Selection.activeTransform != null;
    }

    [MenuItem("GameObject/创建默认 UI/ScrollView/Tmp_ScrollView", priority = -97)]
    static void CreateTmpScrollView()
    {
        if (Selection.activeTransform)
        {
            var go = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Game/Repository/Prefabs/UIForms/_Common/Tmp_ScrollView.prefab");
            go = GameObject.Instantiate(go);
            go.name = "SP_View";

            go.transform.SetParent(Selection.activeTransform, false);

            Selection.activeTransform = go.transform;
        }
    }

    [MenuItem("GameObject/创建默认 UI/ScrollView/Tmp_ScrollView", true)]
    static bool CreateTmpScrollViewValidate()
    {
        return Selection.activeTransform != null;
    }

    [MenuItem("GameObject/创建默认 UI/AlphaMask", priority = -96)]
    static void CreateAlphaMask()
    {
        if (Selection.activeTransform)
        {
            var go = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Game/Repository/Prefabs/UIForms/_Common/AlphaMask.prefab");
            go = (GameObject)PrefabUtility.InstantiatePrefab(go);
            go.name = "AlphaMask";

            go.transform.SetParent(Selection.activeTransform, false);

            Selection.activeTransform = go.transform;
        }
    }

    [MenuItem("GameObject/创建默认 UI/AlphaMask", true)]
    static bool CreateAlphaMaskValidate()
    {
        return Selection.activeTransform != null;
    }

    [MenuItem("GameObject/创建默认 UI/HelpTip", priority = -95)]
    static void CreateHelpTip()
    {
        if (Selection.activeTransform)
        {
            var go = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Game/Repository/Prefabs/UIForms/_Common/HelpTip.prefab");
            go = (GameObject)PrefabUtility.InstantiatePrefab(go);
            go.name = "Btn_Rule";

            go.transform.SetParent(Selection.activeTransform, false);

            Selection.activeTransform = go.transform;
        }
    }

    [MenuItem("GameObject/创建默认 UI/HelpTip", true)]
    static bool CreateHelpTipValidate()
    {
        return Selection.activeTransform != null;
    }


    [MenuItem("GameObject/创建默认 UI/ReuseList", priority = -94)]
    static void CreateReuseList()
    {
        if (Selection.activeTransform)
        {
            var go = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Resources/Prefabs/UI/Common/ReuseList.prefab");
            go = Object.Instantiate(go);
            go.name = "RL_ReuseList";

            go.transform.SetParent(Selection.activeTransform, false);

            Selection.activeTransform = go.transform;
        }
    }

    [MenuItem("GameObject/创建默认 UI/ReuseList", true)]
    static bool CreateReuseListValidate()
    {
        return Selection.activeTransform != null;
    }


    [MenuItem("GameObject/创建默认 UI/ReuseListScroll", priority = -93)]
    static void CreateReuseListScroll()
    {
        if (Selection.activeTransform)
        {
            var go = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Resources/Prefabs/UI/Common/ReuseListScroll.prefab");
            go = Object.Instantiate(go);
            go.name = "RL_ReuseList";

            go.transform.SetParent(Selection.activeTransform, false);

            Selection.activeTransform = go.transform;
        }
    }

    [MenuItem("GameObject/创建默认 UI/ReuseListScroll", true)]
    static bool CreateReuseListScrollValidate()
    {
        return Selection.activeTransform != null;
    }

    [MenuItem("GameObject/创建默认 UI/UrlImage (Url配置在RemoteImages表里)", priority = -92)]
    static void CreateUrlImage()
    {
        if (Selection.activeTransform)
        {
            var go = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Game/Repository/Prefabs/UIForms/_Common/UrlImage.prefab");
            go = Object.Instantiate(go);
            go.name = "UrlImg_";

            go.transform.SetParent(Selection.activeTransform, false);

            Selection.activeTransform = go.transform;
        }
    }

    [MenuItem("GameObject/创建默认 UI/UrlImage (Url配置在RemoteImages表里)", true)]
    static bool CreateUrlImageValidate()
    {
        return Selection.activeTransform != null;
    }
}